/**
 * Created by TRwin7SP1 on 2016/8/15.
 */

angular.module('inspinia')
  .controller('menuController', function ($scope,roles,$state,$rootScope) {


    function saveRole(roleName){
      var _index = -1;
      $('body').toggleClass('mini-navbar');
      $rootScope.sideBarHidden = true;
      $scope.loginedUser.role = roleName;
      localStorage["loginUser"]=JSON.stringify($scope.loginedUser);
      $scope.nav = roles[$scope.loginedUser.role];
      var user = JSON.parse(localStorage.getItem('user'));

      //当用户且为其他角色时，若没有defMenu相应的页面，则显示系统默认主页
      $scope.pages = roles[$scope.loginedUser.role].menus;
      for(_index=0;_index<$scope.pages.length;_index++){
        if($scope.pages[_index].sref ==user.defMenu){
          break;
        }
      }
      $scope.nav.defaultMenu = (_index >-1 && _index<$scope.pages.length) ? user.defMenu : $scope.nav.defaultMenu;
      $state.go($scope.nav.defaultMenu);
    }
    $scope.showDetail = function () {
      $('body').toggleClass('mini-navbar');
    };
    var _user = localStorage["loginUser"];
    if (_user) {
      $scope.loginedUser= JSON.parse(_user);
    }else {
      $scope.loginedUser= my;
      localStorage["loginUser"]=JSON.stringify(my);
    }
    // console.log("$scope.loginedUser:"+JSON.stringify($scope.loginedUser,null,4));
    $scope.nav = roles[$scope.loginedUser.role];
    $scope.changeRole=function (roleIndex) {
      switch (roleIndex)
      {
        case "admin":
          saveRole("admin");
          break;
        case "demo":
          saveRole("demo");
          break;
        case "register":
          saveRole("register");
          break;
        case "makingModel":
          saveRole("makingModel");
          break;
        case "CTposition":
          saveRole("CTposition");
          break;
        case "Contouring":
          saveRole("Contouring");
          break;
        case "EBP":
          saveRole("EBP");
          break;
        case "validate":
          saveRole("validate");
          break;
        case "charging":
          saveRole("charging");
          break;
        case "treatment":
          saveRole("treatment");
          break;
        case "template":
          saveRole("template");
          break;
      }
    }
  });
